﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmTJ : Form
    {
        private Form1 frmMain = null;
        public FrmTJ(Form1 _frm)
        {
            InitializeComponent();
            frmMain = _frm;
        }

        private void FrmTJ_Load(object sender, EventArgs e)
        {
            tj();
        }

        private void tj()
        {
             string sql1 = "select a.RFID,'车辆' as InOutType,b.CarState as State,b.CarName as Name,a.EnterTime,a.OutTime,a.FoamSurplus,a.OilSurplus " +
                          "  from InOutInfo a inner join dbo.CarInfo b on a.RFID = b.RFID  where CarState='×' or CarState='√' " +
                          " union select RFID,'人员' as InOutType,State,UserName as Name,EnterTime,OutTime,null as FoamSurplus,null as OilSurplus " +
                          " from userinfo where state='×' or state='√' ";
             SqlDataReader reader1 = null;
             SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
             reader1 = cmd1.ExecuteReader();

             LVTJ.Clear();

             LVTJ.BeginUpdate();
             LVTJ.View = View.Details;

             #region 增加Item的標題，共有三個列
             LVTJ.Columns.Add("名称", 100);
             LVTJ.Columns.Add("类型", 40);
             LVTJ.Columns.Add("状态", 60);
             LVTJ.Columns.Add("进场时间", 120);
             LVTJ.Columns.Add("归队时间", 120);
             LVTJ.Columns.Add("RFID", 0);
             //LVTJ.Columns.Add("剩余泡沫", 70);
             //LVTJ.Columns.Add("剩余油料", 70);
             #endregion

             while (reader1.Read())
             {
                 string rfid = reader1["RFID"].ToString();
                 string name = reader1["Name"].ToString();
                 string type = reader1["InOutType"].ToString();
                 string state = reader1["State"].ToString();
                 string EnterTime = reader1["EnterTime"].ToString();
                 string OutTime = reader1["OutTime"].ToString();
                 //string foam = reader1["FoamSurplus"].ToString();
                 //string oil = reader1["OilSurplus"].ToString();

                 LVTJ.Items.Add(rfid, name, 0);
                 LVTJ.Items[rfid].SubItems.Add(type);

                 LVTJ.Items[rfid].SubItems.Add(state);
                 LVTJ.Items[rfid].SubItems.Add(EnterTime);
                 LVTJ.Items[rfid].SubItems.Add(OutTime);
                 LVTJ.Items[rfid].SubItems.Add(rfid);
                 //LVTJ.Items[rfid].SubItems.Add(foam);
                 //LVTJ.Items[rfid].SubItems.Add(oil);
             }
             reader1.Close();


             foreach (ListViewItem lvItems in LVTJ.Items)
             {
                 if (lvItems.SubItems[2].Text == "√")
                 {
                     //判断车内设备情况
                     if (lvItems.SubItems[1].Text == "车辆")
                     {
                         string sql = "select count(*) as aCount from DeviceInfo where  CarRFID='" + lvItems.Name + "' and RFIDState='×'";
                         SqlDataReader reader = null;
                         SqlCommand cmd = new SqlCommand(sql, frmMain.dbConnect);
                         reader = cmd.ExecuteReader();
                         //string i = reader["aCount"].ToString();
                         string i = "1";
                         if (Convert.ToInt32(i) > 0)
                         {
                             lvItems.ForeColor = Color.Yellow;
                         }
                         reader.Close();
                     }
                     else
                     {
                         lvItems.ForeColor = Color.Green;//字体颜色
                     }
                     //lvItems.Font = new System.Drawing.Font(lvItems.Font.FontFamily, 50f);
                 }
                 else
                 {
                     lvItems.ForeColor = Color.Red;//字体颜色
                 }
             }

             LVTJ.EndUpdate();
        }

        private void btnRefrse_Click(object sender, EventArgs e)
        {
            tj();
        }

        //显示车上设备信息
        private void LVTJ_Click(object sender, EventArgs e)
        {
            if (LVTJ.SelectedItems[0].SubItems[1].Text == "车辆")
            {
                string rfid = LVTJ.SelectedItems[0].SubItems[5].Text;
                if (rfid == "") return;

                LVDevice.Visible = true;
                btnClose.Visible = false;
                //lbCareInfo.Text = LVTJ.SelectedItems[0].SubItems[0].Text;

                string sql1 = "select RFID,CarRFID,DeviceName,DeviceType,RFIDState,EnterTime,OutTime,Note" +
                          " from DeviceInfo where CarRFID='" + rfid + "'";
                SqlDataReader reader1 = null;
                SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
                reader1 = cmd1.ExecuteReader();

                LVDevice.Clear();

                LVDevice.BeginUpdate();
                LVDevice.View = View.Details;

                #region 增加Item的標題，共有三個列
                LVDevice.Columns.Add("名称", 120);
                LVDevice.Columns.Add("类型", 60);
                LVDevice.Columns.Add("状态", 40);
                #endregion

                while (reader1.Read())
                {
                    rfid = reader1["RFID"].ToString();
                    string name = reader1["DeviceName"].ToString();
                    string type = reader1["DeviceType"].ToString();
                    string state = reader1["RFIDState"].ToString();

                    LVDevice.Items.Add(rfid, name, 0);
                    LVDevice.Items[rfid].SubItems.Add(type);
                    LVDevice.Items[rfid].SubItems.Add(state);
                }
                reader1.Close();

                foreach (ListViewItem lvItems in LVDevice.Items)
                {
                    if (lvItems.SubItems[2].Text == "√")
                    {
                        lvItems.ForeColor = Color.Green;//字体颜色
                    }
                    else
                    {
                        lvItems.ForeColor = Color.Red;//字体颜色
                    }
                }
                LVDevice.EndUpdate();
            }
            else
            {
                LVDevice.Visible = false;
                btnClose.Visible = false;
            }
        }

        private void LVDevice_Click(object sender, EventArgs e)
        {
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            LVDevice.Visible = false;
            btnClose.Visible = false;
        }

        private void LVTJ_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmTJ_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmTJ", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmTJ", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
